#!/bin/bash -e
# mtomcat.sh -- manage tomcats

# Copyright (C) 2011 Friedrich Gonzalez
#
#   This file is part of multitomcat
#
#   multitomcat is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   multitomcat is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
#

exe="$0"

. $(dirname $exe)/../etc/multitomcat.cfg

Error[0]="Unknown Error"
Error[1]="Tomcat version already downloaded"
Error[2]="Error using wget. Is the network available?. Is the downloadhost available?"
Error[3]="Wrong Checksum. The file downloaded didn't match the checksum. Try downloading again."
Error[4]="Failed to decompress tar file"
Error[5]="Failed to move version file"
Error[6]="Failed to remove tar file"
Error[7]="Failed to remove version directory"
Error[8]="You must provide an instance name"
Error[9]="That tomcat version is not in the repository"
Error[10]="Yout must provide an app name"
Error[11]="You must provide a version"
Error[12]="The instance name you provided does not exist"
Error[13]="Can't delete the instance"
Error[14]="The instance you provided already exists"

function quitError {
        echo "Error: ${Error[$1]}"
	exit
}

function listinstances {
	list="$(ls -1 $installdir/instance)"|| true
        [[ -z "$list" ]] && list="No instances created"
        echo "$list"
}

function listversions {
	list="$(ls -1 $installdir/version)" || true
	[[ -z "$list" ]] && list="No versions installed"
	echo "$list"
}

function listapps {
	list="$(ls -1 $installdir/instance/*/webapps/* 2>/dev/null)" || true
	[[ -z "$list" ]] && list="No apps deployed"
	echo "$list"
}

function createinstance {
	version=$1
	instance=$2
	[[ -z "$version" ]] && quitError 10
	[[ -z "$instance" ]] && quitError 8
	[[ -e "$installdir/instance/$instance" ]] && quitError 14
	echo "not implemented"
}

function deleteinstance {
	instance=$1
	[[ -z "$instance" ]] && quitError 8
	[[ ! -e "$installdir/instance/$instance" ]] && quitError 12
	echo "Deleting instance $instance"
	! rm -rf "$installdir/instance/$instance" && quitError 13
	echo "Instance $instance deleted"
}

function startinstance {
	instance=$1
	[[ -z "$instance" ]] && quitError 8
	[[ ! -e "$installdir/instance/$instance" ]] && quitError 12
	echo "not implemented"
}

function stopinstance {
	instance=$1
	[[ -z "$instance" ]] && quitError 8
        [[ ! -e "$installdir/instance/$instance" ]] && quitError 12
	echo "not implemented"
}

function deployapp {
	instance=$1
	app=$2
	[[ -z "$instance" ]] && quitError 8
	[[ -z "$app" ]] && quitError 10
        [[ ! -e "$installdir/instance/$instance" ]] && quitError 12
	echo "not implemented"
}

function undeployapp {
	instance=$1
	app=$2
	[[ -z "$instance" ]] && quitError 8
	[[ -z "$app" ]] && quitError 10
        [[ ! -e "$installdir/instance/$instance" ]] && quitError 12
	echo "not implemented"
}

function downloadversion {
	# http://archive.apache.org/dist/tomcat/tomcat-6/
	version=$1
  	downloadhost=$2
	downloadhostmd5=$2
	[[ -z "${version}" ]] && version=$cversion
	[[ -z "${downloadhost}" ]] && downloadhost=$cdownloadhost && downloadhostmd5=$cdownloadhostmd5
	[[ -e "$installdir/version/$version" ]] && quitError 1
	tarname=apache-tomcat-$version.tar.gz
	tarfile=$installdir/download/$tarname
	echo "Downloading tomcat $version ..."
	! wget http://$downloadhost/tomcat/tomcat-6/v$version/bin/$tarname -O $tarfile && echo "Perhaps you want to use http://archive.apache.org/dist site" && quitError 2
	! wget http://$downloadhostmd5/tomcat/tomcat-6/v$version/bin/$tarname.md5 -O $tarfile.md5 && quitError 2
	cd $installdir/download
	! md5sum -c ${tarname}.md5 2>&1 1>/dev/null && rm $tarfile && quitError 3
     	! tar -C $installdir/version -xvzf $tarfile && quitError 4
	! mv "$installdir/version/apache-tomcat-$version" "$installdir/version/$version" && quitError 5
	! rm $tarfile && quitError 6
	echo "Tomcat downloaded and deployed in $installdir/version/$version"
}

function deleteversion {
	version=$1
	echo "Deleting tomcat $version"
	[[ -z "$version" ]] &&  version=$cversion
	[[ ! -e "$installdir/version/$version" ]] && quitError 9
	! rm -rf $installdir/version/$version && quitError 7
 	echo "Tomcat version $version deleted"	
}

function showconfig {	
	cat $installdir/etc/multitomcat.cfg	
}

function helpaction {
	option="$1"
	case "$option" in
		downloadversion)
			echo "Usage: $exe downloadversion 6.X.X site"
			echo "Examples:"
			echo "- $exe 6.0.32"
			echo "- $exe 6.0.20 archive.apache.org/dist"
			;;
		deleteversion)
			echo "Usage: $exe deleteversion 6.X.X"
			;;
		*)	
		        echo "options: listapps listinstances listversions createinstance deleteinstance startinstance stopinstance deployapp undeployapp downloadversion deleteversion config help"
			;;
	esac
}

action=$1


case "$action" in
   listapps)
     listapps;
    ;;
   listinstances)
     listinstances;
     ;;
   listversions)
     listversions;
     ;;
   createinstance)
     createinstance $2 $3;
     ;;
   deleteinstance)
     deleteinstance $2;
     ;;
   startinstance)
     startinstance $2 ;
     ;;
   deployapp)
     deployapp $2 $3;
     ;;
   undeployapp)
     undeployapp $2 $3;
     ;;
   downloadversion)
     downloadversion $2 $3;
     ;;
   deleteversion)
     deleteversion $2;
     ;;
   showconfig)
     showconfig;
     ;;
   *)
     helpaction $2;
     ;;
esac


